package com.tengju.bff.interfaces.manage.crm.user.assembler;

import com.tengju.bff.interfaces.manage.crm.user.vo.*;
import com.tengju.bff.interfaces.manage.star.vo.SurveyConfigVO;
import com.tengju.bff.interfaces.manage.star.vo.SurveyQuestionConfigContentVO;
import com.tengju.bff.interfaces.manage.star.vo.SurveyQuestionConfigVO;
import com.tengju.user.domain.model.survey.*;
import com.tengju.user.domain.model.user.UserIdCode;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

public class ArchiveAssembler {

    public static SurveyContent contentVOToModel(ArchiveContentForm form) {

        List<SurveyQuestionResult> resultList = form.getArchiveResults().stream()
                .map(ArchiveAssembler::convertResult).collect(Collectors.toList());

        return new SurveyContent(new UserIdCode(form.getIdCode()),
                new SurveyId(form.getSurveyId()),
                resultList);
    }

    public static SurveyConfigInfo contentVOToModel(AddSurveyConfigForm form) {


        return new SurveyConfigInfo(MsgType.fromVal(form.getMsgType()),
                new SurveyId(form.getSurveyId()),
                form.getSurveyTitle(),
                form.getItemCode(),
                form.getItemOrder(),
                form.getItemTitle(),
                form.getQueryTitle(),
                form.getItemParam(),
                SurveyQuestionTypeEnum.parseByCode(form.getItemType()),
                form.getItemRequired(),
                "admin",
                "admin"
        );
    }


    public static SurveyConfigInfo contentVOToModel(EditSurveyConfigForm form) {


        return new SurveyConfigInfo(MsgType.fromVal(form.getMsgType()),
                new SurveyId(form.getSurveyId()),
                form.getSurveyTitle(),
                form.getItemCode(),
                form.getItemOrder(),
                form.getItemTitle(),
                form.getQueryTitle(),
                form.getItemParam(),
                SurveyQuestionTypeEnum.parseByCode(form.getItemType()),
                form.getItemRequired(),
                "admin",
                "admin"
        );
    }

    private static SurveyQuestionResult convertResult(ArchiveResult result) {
        return new SurveyQuestionResult(
                null,
                result.getArchiveCode(),
                null,
                result.getArchiveContent(),
                null
        );
    }

    public static SurveyConfigVO configModelToVO(SurveyConfig surveyConfig,List<SurveyQuestionResult> filledOption) {
        if (surveyConfig == null) {
            return null;
        }
        SurveyConfigVO surveyConfigVO = new SurveyConfigVO();
        surveyConfigVO.setSurveyId(surveyConfig.getSurveyId().getValue());
        surveyConfigVO.setSurveyTitle(surveyConfig.getSurveyTitle());
        surveyConfigVO.setSurveyDesc(surveyConfig.getSurveyDesc());
        List<SurveyQuestionConfig> questionConfigList = surveyConfig.getQuestionConfigList();
        List<SurveyQuestionConfigVO> questionConfigVOList = new ArrayList<>();

        Map<String, SurveyQuestionResult> filledMap = filledOption.stream().collect(Collectors.toMap(SurveyQuestionResult::getQuestionCode, Function.identity()));

        for (int i = 0; i < questionConfigList.size(); i++) {
            SurveyQuestionConfig v = questionConfigList.get(i);
            SurveyQuestionConfigVO questionConfigVO = new SurveyQuestionConfigVO();
            questionConfigVO.setCode(v.getCode());
            questionConfigVO.setOrder(i + 1);
            questionConfigVO.setRequired(v.getRequired().equals(SurveyQuestionRequireEnum.REQUIRED));
            questionConfigVO.setTitle(v.getTitle());
            questionConfigVO.setType(v.getType().getCode());
            questionConfigVO.setHasFilled(false);
            questionConfigVO.setMsgType(Optional.ofNullable(v.getMsgType()).map(MsgType::getVal).orElse(null));

            //多文本框特处理
            if (v.getType() == SurveyQuestionTypeEnum.MANY_TEXT) {

                List<String> titles = v.getConfigContentList().stream().map(SurveyQuestionConfigContent::getTitle).collect(Collectors.toList());
                List<SurveyQuestionResult> results = titles.stream()
                        .map(filledMap::get).collect(Collectors.toList());
                SurveyQuestionResult otherResult = filledMap.get("其他媒体号");

                String filledData = "";

                if (!CollectionUtils.isEmpty(results)) {
                    StringBuilder resultBuild = new StringBuilder();
                    for (SurveyQuestionResult result : results) {
                        if (result != null && !StringUtils.isEmpty(result.getQuestionCode()) && !StringUtils.isEmpty(result.getQuestionContent())) {
                            resultBuild.append(result.getQuestionContent()).append("（").append(result.getQuestionCode()).append("）,");
                        }
                    }
                    String resultStr = resultBuild.toString();
                    if (!StringUtils.isEmpty(resultStr)) {
                        filledData = resultStr.substring(0,resultStr.length()-1);
                    }

                }

                if ((!StringUtils.isEmpty(filledData))
                        || (otherResult != null && !StringUtils.isEmpty(otherResult.getQuestionContent()))) {
                    questionConfigVO.setHasFilled(true);
                    questionConfigVO.setFilledData(filledData);
                    questionConfigVO.setOtherData(Optional.ofNullable(otherResult).map(SurveyQuestionResult::getQuestionContent).orElse(""));
                }

            } else if (v.getType() == SurveyQuestionTypeEnum.TRUE_OR_FALSE_DEFALT_FALSE) {
                questionConfigVO.setHasFilled(true);
                SurveyQuestionResult result = filledMap.get(v.getCode());
                if (result == null || StringUtils.isEmpty(result.getQuestionContent())) {
                    questionConfigVO.setFilledData("否");
                } else {
                    questionConfigVO.setFilledData(result.getQuestionContent());
                }
            } else {
                SurveyQuestionResult result = filledMap.get(v.getCode());
                SurveyQuestionResult otherResult = filledMap.get(v.getCode() + "：用户填写");
                if ((result != null && !StringUtils.isEmpty(result.getQuestionContent()))
                        || (otherResult != null && !StringUtils.isEmpty(otherResult.getQuestionContent()))) {
                    questionConfigVO.setHasFilled(true);
                    questionConfigVO.setFilledData(Optional.ofNullable(result).map(SurveyQuestionResult::getQuestionContent).orElse(""));
                    questionConfigVO.setOtherData(Optional.ofNullable(otherResult).map(SurveyQuestionResult::getQuestionContent).orElse(""));
                }
            }


            List<SurveyQuestionConfigContent> configContentList = v.getConfigContentList();
            if (!CollectionUtils.isEmpty(configContentList)) {
                List<SurveyQuestionConfigContentVO> questionConfigContentVOList = configContentList.stream()
                        .map(configContent -> SurveyQuestionConfigContentVO.builder()
                                .code(configContent.getCode())
                                .title(configContent.getTitle())
                                .msg(configContent.getMsg())
                                .pictureUrl(configContent.getPictureUrl())
                                .build())
                        .collect(Collectors.toList());
                questionConfigVO.setConfigContent(questionConfigContentVOList);
            }
            questionConfigVOList.add(questionConfigVO);
        }
        surveyConfigVO.setQuestionConfigList(questionConfigVOList);
        return surveyConfigVO;
    }



    public static ArchiveContentVo contentModelToVO(SurveyQuestionResult result) {

        ArchiveContentVo archiveContentVo = new ArchiveContentVo();

        archiveContentVo.setItemOrder(result.getQuestionOrder());
        archiveContentVo.setTitle(result.getQueryTitle());
        archiveContentVo.setContent(result.getQuestionContent());

        return archiveContentVo;
    }

}
